package com.example.exam.user.service;

import com.example.exam.db.PageInfoBean;
import com.example.exam.shiro.Digests;
import com.example.exam.shiro.EncodeUtils;
import com.example.exam.user.dao.UserDao;
import com.example.exam.user.model.UserBean;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class UserService {
    @Autowired
    UserDao userDao;
    /**
     * @Description:
     * @Param: [ub]
     * @return: java.lang.Integer
     * @Author: xhw
     * @Date: 2021/7/26
     */public Integer login(UserBean ub){
        UserBean uib = userDao.selectLogin(ub.getLogin_name());
        if(uib!=null){
            try{
                String pwd=null;
                pwd=entryptPassword(ub.getPassword(),uib.getSalt());
                ub.setPassword(pwd);
                //System.out.println(uib.getPassword()+"  "+ub.getPassword());
                if (uib.getPassword().equals(ub.getPassword())) {
                    return 1;
                } else {
                    return 0;
                }
            }catch (Exception e){
                return 0;
            }
        }else{
            return 0;
        }

    }
    /**
     * @Description:
     * @Param: [username]
     * @return: java.lang.Integer
     * @Author: xhw
     * @Date: 2021/7/26
     */ public UserBean selectId(String username){
        try{
            UserBean userBean= userDao.selectId(username);
            return userBean;
        }catch (Exception e){
            return null;
        }
    }/**
     * @Description:
     * @Param: [limit, page, loginName, row, id]
     * @return: com.example.exam.db.PageInfoBean
     * @Author: xhw
     * @Date: 2021/7/26
     */
    public PageInfoBean list(Integer limit, Integer page, String loginName,Integer row,Integer id) {
        PageInfoBean pageInfoBean = new PageInfoBean();
        try {
            pageInfoBean.setCurPage(page);
            pageInfoBean.setPerPage(limit);
            return userDao.list(pageInfoBean, loginName,row,id);
        } catch (Exception e) {
            e.fillInStackTrace();
            return null;
        }
    }/**
     * @Description:
     * @Param: []
     * @return: java.util.List<com.example.exam.user.model.UserBean>
     * @Author: xhw
     * @Date: 2021/7/26
     */
    public List<UserBean> listUser(){
        List list=userDao.listUser();
        if(list!=null){
            return list;
        }else{
            return null;
        }
    } /**
     * @Description:
     * @Param: [uib]
     * @return: java.lang.Integer
     * @Author: xhw
     * @Date: 2021/7/26
     */
    public Integer updateUser(UserBean uib) {
        Integer i = userDao.updateAllFieldById(uib);
        if (i > 0) {
            return 1;
        } else {
            return 0;
        }
    }/**
     * @Description:
     * @Param: [id]
     * @return: com.example.exam.user.model.UserBean
     * @Author: xhw
     * @Date: 2021/7/26
     */
    public UserBean selectOneUser(Integer id) {
        UserBean uib = userDao.findById(id);
        return uib;
    }/**
     * @Description:
     * @Param: [uib]
     * @return: java.lang.Integer
     * @Author: xhw
     * @Date: 2021/7/26
     */
    public Integer insertUser(UserBean uib){
        Integer i=userDao.add(uib);
        if(i>0){
            return 1;
        }else{
            return 0;
        }
    } /**
     * @Description:
     * @Param: [id]
     * @return: java.lang.Integer
     * @Author: xhw
     * @Date: 2021/7/26
     */
    public Integer delectUser(Integer id) {
        Integer i = userDao.removeById(id);
        if (i > 0) {
            return 1;
        } else {
            return 0;
        }
    }/**
     * @Description:
     * @Param: [uib]
     * @return: java.lang.Integer
     * @Author: xhw
     * @Date: 2021/7/26
     */
    public Integer updateLoginTime(UserBean uib){
        Integer i=userDao.updateLoginTime(new Date(),uib.getId());
        if(i>0){
            return 1;
        }else{
            return -1;
        }
    }
    private static final int SALT_SIZE = 10;
    /**
     * @Description:
     * @Param: [user]
     * @return: void
     * @Author: xhw
     * @Date: 2021/7/26
     */ public void entryptPassword(UserBean user) {

        byte[] salt = Digests.generateSalt(SALT_SIZE);
        user.setSalt(EncodeUtils.hexEncode(salt));
        // 加密次数
        int hashIterations = 2;
        SimpleHash sh = new SimpleHash("md5", user.getPassword(), EncodeUtils.hexEncode(salt), hashIterations);
        String strsh = sh.toHex();
        user.setPassword(strsh);
    }/**
     * @Description:
     * @Param: [pwd, salt]
     * @return: java.lang.String
     * @Author: xhw
     * @Date: 2021/7/26
     */
    public String entryptPassword(String pwd,String salt) {
        // 加密次数
        int hashIterations = 2;
        SimpleHash sh = new SimpleHash("md5", pwd, salt, hashIterations);
        String strsh = sh.toHex();
        return strsh;
    }/**
     * @Description:
     * @Param: [id, type]
     * @return: java.lang.Integer
     * @Author: xhw
     * @Date: 2021/7/26
     */
    public Integer editType(Integer id,Integer type){
        return userDao.editType(id,type);
    }/**
     * @Description:
     * @Param: [newPwd, salt, id]
     * @return: java.lang.Integer
     * @Author: xhw
     * @Date: 2021/7/26
     */
    public Integer editPwd(String newPwd,String salt,Integer id){
        return userDao.editPwd(newPwd,salt,id);
    }
    @Transactional
    public Integer updateLoginUser(UserBean userBean) {
        try {
            return userDao.updateByIdSelective(userBean);
        } catch (Exception e) {
            e.fillInStackTrace();
            return 0;
        }
    }
}
